package com.sensor.data.service;

import com.sensor.data.assist.ReceiveThread;
import com.sensor.data.component.PortComponent;
import com.sensor.data.dao.SensorToDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;

/**
 * FileName: SensorDataService
 * Create by fengzheng on 2019/4/11 10:10
 * Description: 接收数据服务类
 */

@Service
public class SensorDataService {

    private PortComponent portComponent;
    private SensorToDB sensorToDB;

    @Autowired
    public SensorDataService(PortComponent mPortComponent,SensorToDB mSensorToDB) {
        this.portComponent = mPortComponent;
        this.sensorToDB = mSensorToDB;
    }


    public void startReceive(){
        ArrayList<Integer> portList = portComponent.getPort();

        try {
            InetAddress inetAddress = InetAddress.getLocalHost();
            System.out.println("本机IP："+inetAddress.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
            System.out.println("无法获取到本机IP地址");
        }

        for(Integer port : portList){
            ReceiveThread receiveThread = new ReceiveThread(port,sensorToDB);
            receiveThread.start();
        }
    }
}
